package com.mapper;

import com.entity.Employee;
import com.entity.Salary;
import com.form.EmployeeInfo;
import com.form.EmployeeInfoForAdmin;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 员工表数据层
 *
 * @author z 'y
 */
@Mapper
public interface EmployeeMapper {
    /**
     * 删除员工通过用户名
     *
     * @param username the username
     * @return the int
     */
    @Update("update employee set isExpire = 1 , departmentId = 1 where username=#{username}")
    int deleteEmployeeByUsername(String username);

    /**
     * 通过用户名和登陆密码查找一个员工实体
     *
     * @param username the username
     * @param password the password
     * @return the employee
     */
    @Select("select * from employee where username = #{param1} and password=#{param2}")
    Employee findByUserNameAndPwd(String username, String password);

    /**
     * 查找一个员工的所有工资记录
     *
     * @param employeeId the employee id
     * @return the list
     */
    @Select("select * from salary where employeeId = #{param1}")
    List<Salary> checkSalarysByName(int employeeId);

    /**
     * 查看员工所有的工资记录通过员工id，并实现分页
     *
     * @param employeeId the employee id
     * @param startIndex the start index
     * @param pageSize   the page size
     * @return the list
     */
    @Select("select * from salary where employeeId = #{param1} LIMIT #{param3} OFFSET #{param2}")
    List<Salary> checkSalarysOrderByName(int employeeId, int startIndex, int pageSize);

    /**
     * 获取一个与员工展示实体通过用户名
     *
     * @param username the username
     * @return the employee info by name
     */
    @Select("select username,fullname,email,phoneNumber,birthday,departmentName,levelName,bankAccount,photo" +
            " from Employee natural join department natural join level l where username=#{username}")
    EmployeeInfo getEmployeeInfoByName(String username);

    /**
     * 更新员工信息
     *
     * @param employee the employee
     * @return the int
     */
    @Update("update employee set fullname=#{fullname},birthday=#{birthday},email=#{email},phoneNumber=#{phoneNumber},bankAccount=#{bankAccount} where username=#{username}")
    int updateEmployeeInfo(Employee employee);

    /**
     * 获取员工数量
     *
     * @return the int
     */
    @Select("select count(employeeId) from employee")
    int count();

    /**
     * 获取所有员工信息列表，并实现分页
     *
     * @param startIndex the start index
     * @param pageSize   the page size
     * @return the list
     */
    @Select("select username,fullname,email,phoneNumber,birthday,departmentName,levelName,bankAccount,employTime,isExpire " +
            "from Employee natural join Level natural join Department LIMIT #{param2} OFFSET #{param1}")
    List<EmployeeInfoForAdmin> checkAllEmployees(int startIndex, int pageSize);

    /**
     * 获取当前部门id下的员工数量
     *
     * @param departmentId the department id
     * @return the employee num by depart id
     */
    @Select("select count(employeeId) from employee where departmentId=#{departmentId} and isExpire!=1")
    int getEmployeeNumByDepartId(int departmentId);

    /**
     * 获取一个与员工实体通过用户名
     *
     * @param username the username
     * @return the employee by uername
     */
    @Select("select * from employee where username=#{username}")
    Employee getEmployeeByUername(String username);
    @Insert("insert into employee(username,password,departmentId,levelId,employTime,photo,isExpire) values " +
            "(#{username},#{password},#{departmentId},#{levelId},#{employTime},#{photo},#{isExpire})")
    int addEmployee(Employee employee);
    @Select("select count(*) from Employee natural join Level natural join Department where username like concat('%',#{keys},'%') " +
            "or departmentName like concat('%',#{keys},'%') or levelName like concat('%',#{keys},'%')")
    int getCountOfDepartByKeys(String keys);
    @Select("select username,fullname,email,phoneNumber,birthday,departmentName,levelName,bankAccount,employTime,isExpire " +
            "from Employee natural join Level natural join Department where username like concat('%',#{param1},'%') " +
            "or departmentName like concat('%',#{param1},'%') or levelName like concat('%',#{param1},'%') LIMIT #{param3} OFFSET #{param2}")
    List<EmployeeInfoForAdmin> findPageByKeys(String keys, int startIndex, int pageSize);
    @Update("update employee set departmentId = #{param2},levelId = #{param3} where username=#{param1}")
    int updateEmployeeForAdmin(String username, int departmentId, int levelId);
    /**
     * 通过员工level返回数量
     *
     * @param level the employee level
     * @return the department
     */
    @Select("select count(*) from employee where levelId=#{level}")
    int countOfEmployeeByLevel(int level);
    @Update("update employee set photo = #{param2} where username=#{param1}")
    int updatePhotoByUsername(String username, String url);
}